﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

/// <summary>
/// Summary description for AuthorizationUtil
/// </summary>
public class AuthorizationUtil
{
    public AuthorizationUtil()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public static List<Function> GetRoleFunctionForUser(string username)
    {
        DBDataContext db = new DBDataContext();
        User userRole = db.Users.FirstOrDefault(x => x.UserName == username);
        if (userRole != null && userRole.RoleID == 1)
        {
            return db.Functions.ToList();
        }

        var funcs = (from a in db.RoleFunctions
                     join b in db.Users on a.RoleID equals b.RoleID
                     join c in db.Functions on a.FunctionID equals c.ID
                     where b.UserName == username
                     && c.IsCheck == true
                     select c).ToList();
        return funcs;

    }

    //check user co duoc quyen truy cap chuc nang nay hay khong
    public static bool HasAuthorizationForm(string username, string methodName)
    {
        bool hasAuthorization = false;
        DBDataContext db = LINQUtil.GetDBReadOnly();

       // Function f = GetRoleFunctionForUser(username).FirstOrDefault(x => x.MethodName == methodName);
        User userRole = db.Users.FirstOrDefault(x => x.UserName == username);
        if ((userRole != null && userRole.RoleID == 1) || methodName == "default.aspx" || methodName.ToLower()=="welcome")
        {
            return true;
        }

        //Function f2 = db.Functions.FirstOrDefault(x => x.MethodName == methodName);
        var f2 = (from a in db.RoleFunctions
                     join b in db.Users on a.RoleID equals b.RoleID
                     join c in db.Functions on a.FunctionID equals c.ID
                     where b.UserName == username
                    // && c.IsCheck == true
                     && c.MethodName == methodName
                     select c).FirstOrDefault();
        if (f2 != null)
        {
            return true;
        }
        else
        {
            Function f1 = db.Functions.FirstOrDefault(x => x.MethodName == methodName && x.IsCheck == false);
            if (f1 != null)
            {
                return true;
            }
        }
        return hasAuthorization;
    }

    /*
    public static bool AuthorizationForm(Control control, string username, string methodName)
    {
        DBDataContext db = LINQUtil.GetDBReadOnly();
        Function f = GetRoleFunctionForUser(username).FirstOrDefault(x => x.MethodName == methodName);
        UserRole userRole = db.UserRoles.FirstOrDefault(x => x.UserName == username);
        if ((userRole != null && userRole.RoleID == 1) || methodName == "default2.aspx")
        {
            return true;
        }

        if (f != null && !String.IsNullOrEmpty(username))
        {
            List<RoleAction> ra = GetRoleActionForUser(username);
            if (ra!=null && ra.Count>0)
            {
                List<RoleAction> rf = ra.Where(x => x.FunctionID == f.ID).ToList();
                bool isDelete = true, isEdit = true, isDetail = true;
                foreach (ComboItem item in RoleActionButtonID.Items)
                {
                    string id = item.Value;
                    RoleAction btn = rf.FirstOrDefault(x => x.ButtonID == id);
                    bool isVisible = btn != null ? btn.Status.Value == ObjectStatus.Active : false;  //default visible
                    int roleId = ra.FirstOrDefault().RoleID.Value;
                    if (roleId==1)
                    {
                        isVisible = true;
                    }
                    Button oBtn = control.FindControl(id) as Button;
                    if (oBtn != null)
                    {
                        oBtn.Text = item.Text;
                        oBtn.Visible = isVisible;
                    }
                    //Danh cho phân quyen grid
                    if (id == "btnDelete")
                    {
                        isDelete = isVisible;
                    }
                    else if (id == "btnEdit")
                    {
                        isEdit = isVisible;
                    }
                    else if (id == "btnDetail")
                    {
                        isDetail = isVisible;
                    }
                }

                RadGrid gv = control.FindControl(RoleActionButtonID.gridView) as RadGrid;
                if (gv != null)
                {
                    GridColumn cd = gv.Columns.FindByUniqueNameSafe("Delete");
                    if (cd != null) cd.Visible = isDelete;

                    GridColumn ce = gv.Columns.FindByUniqueNameSafe("Edit");
                    if (ce != null) ce.Visible = isEdit;

                    GridColumn ct = gv.Columns.FindByUniqueNameSafe("Detail");
                    if (ct != null) ct.Visible = isDetail;
                }
            }
        }
        return HasAuthorizationForm(username,methodName);
    }
    public static void IsVisibleDelete() { 

    }
}

public class RoleActionButtonID {
    public const string gridView = "gv";
    public static List<ComboItem> Items {
        get {
            DBDataContext db = new DBDataContext();
            List<ComboItem> l = new List<ComboItem>();

            if (CacheUtil.GetCache("CodeDetailRoleAction") == null)
            {
                l = (from c in db.CodeDetails
                     where c.CMCode == "RoleAction"
                     select new ComboItem
                     {
                         Text = c.CDName,
                         Value = c.StandardName
                     }).ToList();
            }
            else
            {
                l = (List<ComboItem>)CacheUtil.GetCache("CodeDetailRoleAction");
            }
            return l;
        }
    }

    
}
    */
}